#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Agresso.Interface.Layer.Domain;
using Agresso.Interface.Layer.Service;
using System.Collections;
using System.IO;
using System.Configuration;

#endregion

namespace Agresso.Interface.Layer.Web
{
    public partial class UserControlSoftmarProforma : UserControl
    {
        User m_current_user;
        MainForm m_mainForm;
       // AlertService m_AlertService;
        CommissionInvoiceService m_CommissionInvoiceService;
        string m_startDefault;


        public UserControlSoftmarProforma(string name, User currentUser, MainForm mainForm)
        {
            InitializeComponent();
            m_current_user = currentUser;
            m_mainForm = mainForm;
            m_startDefault = ConfigurationManager.AppSettings["StartPath"];
           // m_AlertService = (AlertService)ServiceFactory.Instance().GetService(ServiceFactory.ALERT_SERVICE);
            m_CommissionInvoiceService = new CommissionInvoiceService();
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
            toolBarButtonExcel.Click += new EventHandler(toolBarButtonExcel_Click);
            toolBarButtonExecute.Click += new EventHandler(toolBarButtonExecute_Click);
            listViewAlert.DoubleClick += new EventHandler(listViewAlert_DoubleClick);
            toolBarButtonRefresh_Click(null, null);
        }

        void toolBarButtonExecute_Click(object sender, EventArgs e)
        {
            IList invoices = new ArrayList();
            foreach (ListViewItem lv in listViewAlert.SelectedItems)
            {
                if (lv.Tag == null) continue;
                invoices.Add(lv.Tag);
            }
            if (invoices.Count == 0)
            {
                MessageBox.Show("Please select at least one invoice to execute.");
                return;
            }
            SoftmarProformaExecuteForm form = new SoftmarProformaExecuteForm(invoices);
            form.ShowDialog();
            foreach (CommissionInvoice inv in invoices)
            {
                foreach (ListViewItem lv in listViewAlert.Items)
                {
                    if (lv.Tag == null) continue;
                    CommissionInvoice ci =  (CommissionInvoice)lv.Tag;
                    if (ci.commission_invoice_id == inv.commission_invoice_id)
                    {
                        listViewAlert.Items.Remove(lv);
                        break;
                    }
                }
            }
           // m_CommissionInvoiceService.ExecuteInvoiceProformaChargeCredit();
           // m_CommissionInvoiceService.ExecuteInvoiceProformaChargeDebit();
        }

        private void Search()
        {
            listViewAlert.Items.Clear();
            statusBar1.Text = "Total : 0 item(s)";
            IList softmarAlert = m_CommissionInvoiceService.GetAllPendingProforma(textBoxSearch.Text);
            LoadItems(softmarAlert);
        }

        void listViewAlert_DoubleClick(object sender, EventArgs e)
        {
            if (listViewAlert.SelectedItem == null) return;
            if (listViewAlert.SelectedItem.Tag == null) return;
            CommissionInvoice a = (CommissionInvoice)listViewAlert.SelectedItem.Tag;
            AlertDetailForm adf = new AlertDetailForm(a, "SH");
            adf.ShowDialog();
        }

        void toolBarButtonExcel_Click(object sender, EventArgs e)
        {
            if (listViewAlert.Items.Count == 0)
            {
                MessageBox.Show("No Data to export, please refresh first.");
                return;
            }
            DataSet ds = new DataSet();
            DataTable dt = ds.Tables.Add("Table");
            foreach (ColumnHeader column in listViewAlert.Columns)
            {
                dt.Columns.Add(column.Text);
            }
            foreach (ListViewItem itm in listViewAlert.Items)
            {
                ArrayList row = new ArrayList();
                foreach (ColumnHeader column in listViewAlert.Columns)
                {
                    row.Add(itm.SubItems[column.Index].Text);
                }
                dt.Rows.Add(row.ToArray());
            }

            System.Web.UI.WebControls.DataGrid grid =
                  new System.Web.UI.WebControls.DataGrid();
            grid.HeaderStyle.Font.Bold = true;
            grid.HeaderStyle.Font.Name = "Calibri";
            grid.HeaderStyle.BackColor = Color.Gold;
            grid.Font.Name = "Calibri";
            grid.Font.Size = 9;
            grid.DataSource = ds;
            grid.DataMember = "Table";
            grid.DataBind();
            string filename = "reportSoftmar" + DateTime.Now.Second.ToString() + ".xls";
            string filePath = m_startDefault + filename;
            using (StreamWriter sw = new StreamWriter(filePath))
            {
                using (System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw))
                {
                    grid.RenderControl(hw);
                }
            }
            Link.Open(filename);
        }

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            listViewAlert.Items.Clear();
            statusBar1.Text = "Total : 0 item(s)";
            IList softmarAlert = m_CommissionInvoiceService.GetAllPendingProforma();
                //m_AlertService.FindAlertByAlertTypeAndStatus(AlertType.SOFTMAR_COMMISSION_INVOICE_TCRELET, AlertStatus.PENDING);
            LoadItems(softmarAlert);
        }

        private void LoadItems(IList softmarAlert)
        {
            foreach (CommissionInvoice a in softmarAlert)
            {
                //string[] alerts = a.remarks.Split(';');
                ListViewItem item = listViewAlert.Items.Add(a.pickup_date == new DateTime(2000, 1, 1) ? "Pending For Execute" : a.pickup_date.ToString("dd-MMM-yyyy")); // alert date..
                item.SubItems.Add(a.invoice_number);
                item.SubItems.Add(a.invoice_date.ToString("dd-MMM-yyyy HH:mm:ss"));
                item.SubItems.Add(a.invoice_amount.ToString("G"));
                item.SubItems.Add(a.profit_centre);
                item.SubItems.Add(a.invoice_type);
                item.SubItems.Add(a.vessel_name);
                item.SubItems.Add(a.voyage_no);
                item.SubItems.Add(a.remarks);
                item.SubItems.Add("Pending");
                item.Tag = a;
            }
            statusBar1.Text = string.Format("Total : {0} item(s)", softmarAlert.Count);
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            Search();
        }

        private void textBoxSearch_EnterKeyDown(object objSender, KeyEventArgs objArgs)
        {
            Search();
        }
    }
}